//
//  PersonalImageViewController.m
//  Weiweiyunshi
//
//  Created by 张旭峰 on 2017/2/6.
//  Copyright © 2017年 王译. All rights reserved.
//

#import "PersonalImageViewController.h"
#import "LoginModel.h"
@interface PersonalImageViewController ()<UIImagePickerControllerDelegate,UIActionSheetDelegate,UINavigationControllerDelegate>

@property(nonatomic,strong)UIActionSheet * actionSheet;
@property(nonatomic,weak)UIImageView* ImageView;
@end

@implementation PersonalImageViewController
@synthesize actionSheet = _actionSheet;
- (void)viewDidLoad {
    [super viewDidLoad];
    
//    self.navigationController.title= @"个人头像";
    self.navigationItem.title = @"个人头像";
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"header_back_icon"] style:UIBarButtonItemStyleDone target:self action:@selector(pop)];
    self.navigationItem.leftBarButtonItem = backItem;
    self.view.backgroundColor = [UIColor grayColor];
    UIImageView * ImageView = [[UIImageView alloc]init];
//    [ImageView sd_setImageWithURL:[NSURL URLWithString:self.image]];
    ImageView.image = self.image;
    
//
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"selectimage"] style:UIBarButtonItemStylePlain target:self action:@selector(select)];
    rightItem.tintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = rightItem;
    
//    button1 addTarget: action:<#(nonnull SEL)#> forControlEvents:<#(UIControlEvents)#>
    
    [self.view addSubview:ImageView];
//    [self.view addSubview:button1];
//     [button1 mas_makeConstraints:^(MASConstraintMaker *make) {
//         make.center.equalTo(self.view);
//         make.left.right.offset(0);
//         make.height.equalTo(@30);
//         
//     }];
//    UIImage * iamge = [UIImage imageNamed:@"email"];
//    [self upDateImage:iamge];
    
    [ImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.offset(0);
        make.height.equalTo(self.view.mas_width);
        make.center.equalTo(self.view);
//        make.width.height.equalTo(@200);
        
        
        
    }];
    self.ImageView = ImageView;
}
-(void)pop
{
    [self.navigationController popViewControllerAnimated:YES];
    
}
-(void)select
{
    [self callActionSheetFunc];
//    [self  dismissViewControllerAnimated:NO]
//    [self dismissModalViewControllerAnimated:NO];
//     completion:^{o
//        
//        NSLog(@"123123123");
//        
//        
//    }];
}
- (void)callActionSheetFunc{
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
        self.actionSheet = [[UIActionSheet alloc] initWithTitle:@"选择图像" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照", @"从相册选择", nil ,nil];
    }else{
        self.actionSheet = [[UIActionSheet alloc] initWithTitle:@"选择图像" delegate:self cancelButtonTitle:@"取消"destructiveButtonTitle:nil otherButtonTitles:@"从相册选择", nil ,nil];
    }
    
    self.actionSheet.tag = 1000;
    [self.actionSheet showInView:self.view];
    
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (actionSheet.tag == 1000) {
        NSUInteger sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        // 判断是否支持相机
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            switch (buttonIndex) {
                case 0:
                    //来源:相机
                    sourceType = UIImagePickerControllerSourceTypeCamera;
                    break;
                case 1:
                    //来源:相册
                    sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                    break;
                case 2:
                    return;
            }
        }
        else {
            if (buttonIndex == 2) {
                return;
            } else {
                sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
            }
        }
        // 跳转到相机或相册页面
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        imagePickerController.delegate = self;
        imagePickerController.allowsEditing = YES;
        imagePickerController.sourceType = sourceType;
        
        [self presentViewController:imagePickerController animated:YES completion:^{
            
        }];
    }
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    
    
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    NSLog(@"---->image1---->%@",image);
    
//    [self upDateImage:image];
    [picker dismissViewControllerAnimated:NO completion:^{
        
        NSLog(@"123123123");
//        [self upDateImage:image];

        
    }];
//    ARPersondataCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
    
    self.ImageView.image = image;
    
    
    if(self.imageblock != nil)
    {
        self.imageblock(image);
        
    }
    NSLog(@"<<<<----->>>>>>>>1");
//    //    NSLog(@"%@",self.cell34);
//    
//    cell.headimagview.image = image;
//    cell.headimagview.layer.cornerRadius =  cell.headimagview.bounds.size.height *.5;
//    cell.headimagview.layer.masksToBounds = YES;
    //    [self.tableView reloadData];
}


//-(void)upDateImage:(UIImage*)photo
//{
//    
//    NSData *data =UIImageJPEGRepresentation(photo,1.0);
//    
//    
//    NSString *dataStr = [data base64EncodedStringWithOptions:0];
//    
//    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//    
//    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
//    
//    
//    NSMutableDictionary *params = [NSMutableDictionary dictionary];
////    params[@"userId"] = @"18513255845";
////    params[@"img"] = dataStr;
//
//    LoginModel *model = [LoginModel sharedInstance].userArr[0];
//    params[@"userId"] = model.userId;
//    params[@"img"] = dataStr;
//    [manager POST:@"http://vvuc.egoonet.com:8088/SSIL/ModifyAvatar.do"parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
//        
//    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        
//        
//        
//        
//        
//        NSLog(@"----------->>.>>>>%@",responseObject[@"Code"]);
//        
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        NSLog(@"%@",error);
//    }];
//    
//    
//    
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}



@end
